package com.slofzx.nuoda.evaluation.version.controller;

import com.slofzx.nuoda.common.controller.BaseController;
import com.slofzx.nuoda.common.dto.FileDTO;
import com.slofzx.nuoda.common.enums.CommonResultEnum;
import com.slofzx.nuoda.common.enums.ServiceResult;
import com.slofzx.nuoda.common.vo.AjaxResultVO;
import com.slofzx.nuoda.common.vo.FileVO;
import com.slofzx.nuoda.common.vo.PageResultVO;
import com.slofzx.nuoda.evaluation.core.util.StringUtil;
import com.slofzx.nuoda.evaluation.project.controller.ReportInfoDTO;
import com.slofzx.nuoda.evaluation.project.dto.ReportInfoVO;
import com.slofzx.nuoda.evaluation.version.dto.CustomFileDTO;
import com.slofzx.nuoda.evaluation.version.service.ProjectReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 评价信息控制层
 *
 * @author nuoda
 * @menu 评价信息
 * @since 2022-08-11 08:55:59
 */
@RestController
@RequestMapping("/project/report")
public class ProjectReportController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private ProjectReportService projectReportService;

    @GetMapping("/data")
    public PageResultVO<List<ReportInfoVO>> queryList(ReportInfoDTO dto) {
        List<ReportInfoVO> dataList = projectReportService.queryList(dto);
        return PageResultVO.newSuccessResult(dataList, dto.getCount());
    }

    @PostMapping("/save")
    public AjaxResultVO save(@RequestBody CustomFileDTO fileDTO) {
        ServiceResult serviceResult =  projectReportService.save(fileDTO);
        return AjaxResultVO.getInstance(serviceResult);
    }
    /**
     * 生成报告
     *
     * @param versionId 参数
     */
    @ResponseBody
    @GetMapping("/createReport")
    public AjaxResultVO<ServiceResult> createReport(@RequestParam String planId,@RequestParam String versionId) {
        ServiceResult result  = projectReportService.createReport(planId,versionId);
        return AjaxResultVO.getInstance(result);
    }

    @ResponseBody
    @GetMapping("/download")
    public ResponseEntity<byte[]> downloadReport(@RequestParam String planId,HttpServletRequest request) {
        FileVO fileVO = projectReportService.downloadReport(planId);
        HttpHeaders headers = new HttpHeaders();
        headers.add("content-disposition",
                "attachment; filename=" + StringUtil.convertDownloadFileName(fileVO.getName(),request.getHeader("User-Agent")));
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(fileVO.getData(), headers, statusCode);
        return entity;
    }
}
